<?php

// @formatter:off
/**
 * @file action.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: action.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/services/rules/remove/action.php $
 */
// @formatter:on

class WCServiceRulesRemoveAction extends WCService {
	//
	// Protected variables.
	//
	// Class methods.
	public function __construct() {
		parent::__construct();

		$this->_requiredParams["url"][] = "group";
		$this->_requiredParams["url"][] = "rule";
		$this->_requiredParams["url"][] = "raction";

		$this->_requiredPermissions["to"][] = WC_PERM_RULE_EDIT;
	}
	//
	// Public methods
	public function check() {
		if(parent::check()) {
			//
			// Loading global pointers.
			global $wcGroup;
			global $wcRule;
			global $wcRuleAction;

			if(!$wcGroup || !$wcGroup->ok()) {
				$this->setError(WC_HTTPERROR_INTERNAL_SERVER_ERROR, "There's something wrong with the specified group id");
			}
			if(!$wcRule || !$wcRule->ok()) {
				$this->setError(WC_HTTPERROR_INTERNAL_SERVER_ERROR, "There's something wrong with the specified rule id");
			}
			if($wcRule && $wcGroup->id() != $wcRule->group()->id()) {
				$this->setError(WC_HTTPERROR_BAD_REQUEST, "Wrong group and rule combinations");
			}
			if(!$wcRuleAction || !$wcRuleAction->ok()) {
				$this->setError(WC_HTTPERROR_INTERNAL_SERVER_ERROR, "There's something wrong with the specified rule action id");
			}
			if($wcRuleAction && $wcRule && $wcRuleAction->rule()->id() != $wcRule->id()) {
				$this->setError(WC_HTTPERROR_BAD_REQUEST, "Wrong rule and rule combinations");
			}
		}

		return $this->ok();
	}
	//
	// Protected methods.
	protected function runPOST() {
		//
		// Loading global pointers.
		global $wcRuleAction;

		return $wcRuleAction->remove();
	}
}
